<?php


namespace app\api\controller;
use app\BaseController;
use OSS\Core\OssException;
use OSS\OssClient;
use think\facade\Filesystem;

class Upload extends BaseController
{
    public function uploadImage()
    {
        $file = request()->file('images');  //input name=image
        //要上传文件的临时路径

        $filePath = $_FILES['images']['tmp_name'];
        return $this->success(ossUpload($filePath));

        //$type = $_FILES['images']['type'];
//        $type = $file->extension();
//        // 尝试执行
//        try {
//            $accessKeyId = Filesystem::getDiskConfig('aliyun_oss','accessKeyId');//去阿里云后台获取秘钥
//            $accessKeySecret =  Filesystem::getDiskConfig('aliyun_oss','accessKeySecret');//去阿里云后台获取秘钥
//            $endpoint =  Filesystem::getDiskConfig('aliyun_oss','endpoint');//你的阿里云OSS地址
//            $bucket =  Filesystem::getDiskConfig('aliyun_oss','bucket');//你的阿里云OSS地址
//            //实例化对象 将配置传入
//            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
//            //这里是有sha1加密 生成文件名 之后连接上后缀
//            $fileName = sha1(date('YmdHis', time()) . uniqid()) . '.' . $type;
//            //上传至阿里云的目录 为年+月/日的格式
//            $pathName = date('Y-m/d') . '/' .$fileName;
//            //执行阿里云上传 bucket名称,上传的目录,文件
//            $result = $ossClient->uploadFile($bucket, $pathName, $filePath);
//        } catch (OssException $e) {
//            return json(['code'=>0,'msg'=>'上传失败','data'=>$e->getMessage()]);
//        }
//        //将结果输出
//        return json(['code'=>200,'msg'=>'上传成功','data'=>$result['info']['url']]);
    }

}